/*

        ABluetooth - Processing for Android Bluetooth Library

        Copyright (c) 2012 Marlon J. Manrique
        
        http://www.marlonj.com

        This library is free software; you can redistribute it and/or
        modify it under the terms of the GNU Lesser General Public
        License as published by the Free Software Foundation; either
        version 2.1 of the License, or (at your option) any later version.

        This library is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        Lesser General Public License for more details.

        You should have received a copy of the GNU Lesser General
        Public License along with this library; if not, write to the
        Free Software Foundation, Inc., 59 Temple Place, Suite 330,
        Boston, MA  02111-1307  USA

	$Id$

*/

package com.marlonj.processing.abluetooth;

import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import com.marlonj.processing.aclientserver.AClient;

/**
 * A bluetooth device 
 */ 
public class ABluetoothDevice {
	
	/**
	 * The native device 
	 */
	private BluetoothDevice device; 

	/**
	 * Create a device with the information of the native device 
	 *
	 * @param device native bluetooth device 
	 */
	public ABluetoothDevice(BluetoothDevice device) {
		this.device = device; 
	}

	/**
	 * Returns the name of the device 
	 * 
	 * @param Name of the device 
	 */
	public String name() {
		return device.getName();
	}

	/**
	 * Returns the address of the device 
	 * 
	 * @param Address of the device 
	 */
	public String address() {
		return device.getAddress();
	}

	/**
	 * Returns the object like string 
	 * 
	 * @return name of the device 
	 */
	public String toString() {
		return name();
	}

	/** 
	 * Connect to the service specified by the UUID 
	 *
	 * @param uuid The identifier of the service 
	 */
	public AClient connect(String uuid) {

		// The client to send and receive data 
		AClient client = null;

		// Try the open the connection and create the client 
		try {
			// Convert the string to a UUID object 
			UUID uuidObject = UUID.fromString(uuid);

			// Open the socket with the service and create a connection 
			BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuidObject);
			ABluetoothConnection connection = new ABluetoothConnection(socket);

			// Create 
			client = new AClient(connection);
		} catch(Exception e) {	
			e.printStackTrace();
		}

		// Return the client, if something fails returns null 
		return client;
	}
}
